
const app = getApp()
import {Date2} from '../../utils/date2.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    family: 0,  //从哪个家族进入
    person: 0,  //当前人的ID
    attrs: [],  //所有属性和值
    scroll_height: 0,  //scroll-view的固定高度，单位像素
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('start personal page', options)
    this.setData({
      family: parseInt(options.family),
      person: parseInt(options.person)
    })

    //请求属性信息
    this.refreshAttr()
  },

  refreshAttr: function() {
    app.httpRequest('get_person_vals', {
      family: this.data.family,
      target_person: this.data.person
    }, resObj => {
      console.log('get attr val list: ', resObj)

      let res = resObj.vals
      //转化数据格式
      for (let i = 0; i < res.length; i++) {
        if (res[i].fix == 1 && res[i].attr_name == '生日') {
          delete res[i].show_value
          if (res[i].value.length > 0) {
            let date2 = new Date2()
            let ok = date2.ParseDbDateStr(res[i].value)
            if (!ok) {
              app.showTip('生日日期格式异常')
            } else {
              res[i].show_value = date2.FormatShowDate()
            }
          }
        }
      }

      this.setData({
        attrs: res
      })
    }, res => {
      console.log('get attr val list failed: ', res)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.createSelectorQuery()
    .select('.attr_layout')
    .fields({
      size: true
    })
    .exec(this.init.bind(this))
  },

  init(res) {
    const width = res[0].width
    const height = res[0].height
    this.setData({
      scroll_height: height
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindTapBirthday(e) {
    console.log("click birthday")
    //传入数据库时间字符串

    let dbDateStr = ''
    let attrs = this.data.attrs
    for (let i = 0; i < attrs.length; i++) {
      if (attrs[i].fix == 1 && attrs[i].attr_name == '生日') {
        dbDateStr = attrs[i].value
      }
    }

    wx.navigateTo({
      url: './edit_date?date=' + dbDateStr,
    })
  },

  editDateCallback(date2) {
    let res = this.data.attrs
    for (let i = 0; i < res.length; i++) {
      if (res[i].fix == 1 && res[i].attr_name == '生日') {
        res[i].value = date2.FormatDbDateStr()
        res[i].show_value = date2.FormatShowDate()
      }
    }
    this.setData({
      attrs: res
    })
  },

  formInputChange(e) {
    let id = e.currentTarget.dataset.field
    let value = e.detail.value
    let items = this.data.attrs
    for (let i = 0; i < items.length; i++) {
      if (items[i].id == id) {
        items[i].value = value
        break
      }
    }
    this.setData({
      attrs: items
    })
  },

  bindTapBack(e) {
    wx.navigateBack()
  },

  bindTapSave(e) {
    let values = []
    let items = this.data.attrs
    for (let i = 0; i < items.length; i++) {
      if (items[i].fix == 1 && items[i].attr_name == '性别') {
      } else {
        values.push(items[i])
      }
    }

    app.httpRequest('set_person_vals', {
      family: this.data.family,
      target_person: this.data.person,
      values: values
    }, res => {
      console.log('update value list: ', res)
      // app.showOkTip("更新成功")

      //返回
      let pages = getCurrentPages()  //获取当前页面js里面的pages里的所有信息。
      let prevPage = pages[pages.length - 2]  //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。
      prevPage.refreshAttr()
      
      wx.navigateBack()
    }, res => {
      console.log('update attr list failed: ', res)
    })
  }
})